package com.bole.controller;


import com.bole.dao.DicBrandDao;
import com.bole.dto.brand.DicBrandDto;
import com.bole.dto.brand.TbCarSeriesDto;
import com.bole.pojo.DicBrandDo;
import com.bole.pojo.TbCarSeriesDo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("brand")
@Api(value = "品牌管理api")
public class BrandController {
    @Autowired
    private DicBrandDao dicBrandDao;

    @GetMapping("getBrandInfo")
    @ApiOperation(value = "查询全部品牌",notes = "查询全部品牌")
    public List<DicBrandDto> getBrandInfo() {
        List<DicBrandDo> all = dicBrandDao.findAll();
        ArrayList<DicBrandDto> arrayList = new ArrayList<>();
        for (DicBrandDo dicBrandDo : all) {
            DicBrandDto brandDto = new DicBrandDto();
            BeanUtils.copyProperties(dicBrandDo, brandDto);
            arrayList.add(brandDto);
        }

        return arrayList;

    }

    @PostMapping("saveBrandInfo")
    @ApiOperation(value = "保存品牌",notes = "保存品牌")
    @ApiImplicitParam(name = "carSeriesDo",value = "品牌对象",required = true,dataTypeClass = DicBrandDo.class)
    public void saveCarSeriesInfo(@RequestBody DicBrandDo dicBrandDo) {
        DicBrandDo brandDo = dicBrandDao.save(dicBrandDo);
        DicBrandDto brandDto = new DicBrandDto();
        BeanUtils.copyProperties(brandDo,brandDto);
    }


    @PostMapping("toUpdate")
    @ApiOperation(value = "回显",notes = "回显")
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataTypeClass = Long.class)
    public DicBrandDto toUpdate(@RequestParam("id") Long id) {
        DicBrandDo brandDo = dicBrandDao.findById(id).get();
        DicBrandDto brandDto = new DicBrandDto();
        BeanUtils.copyProperties(brandDo,brandDto);
        return brandDto;
    }

    @PostMapping("updateBrandInfo")
    @ApiOperation(value = "修改信息",notes = "修改信息")
    @ApiImplicitParam(name = "carSeriesDo",value = "品牌对象",required = true,dataTypeClass = DicBrandDo.class)
    public void updateContentCateInfo(@RequestBody DicBrandDo brandDo) {
        DicBrandDo dicBrandDo = dicBrandDao.save(brandDo);
        DicBrandDto dicBrandDto = new DicBrandDto();
        BeanUtils.copyProperties(dicBrandDo,dicBrandDto);
    }

    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除",notes = "批量删除")
    @ApiImplicitParam(name = "idss",value = "车系对象",required = true,dataTypeClass = Long.class)
    public void deleteBatch(@RequestParam("idss") List<Long> idss) {

        dicBrandDao.deleteBatch(idss);
    }


    //通过firstChar查询
    @GetMapping("getBrandFirstChar")
    @ApiOperation(value = "通过firstChar查询所有品牌",notes = "通过firstChar查询所有品牌")
    @ApiImplicitParam(name = "firstChar",value = "品牌id",required = true,dataTypeClass = String.class)
    public List<DicBrandDto> getBrandFirstChar(@RequestParam String firstChar) {
        List<DicBrandDo> finAllByFirstChar = dicBrandDao.findAllByFirstChar(firstChar);
        ArrayList<DicBrandDto> brandDtos = new ArrayList<>();
        for (DicBrandDo dicBrandDo : finAllByFirstChar) {
            DicBrandDto brandDto = new DicBrandDto();
            BeanUtils.copyProperties(dicBrandDo, brandDto);
            brandDtos.add(brandDto);
        }

        return brandDtos;

    }
}
